#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initModel1(QStandardItemModel *model)
{
    QStandardItem *item1 = new QStandardItem("四年级");

    model->setItem(0,0,item1);
    QStandardItem *item00 = new QStandardItem("张三");
    QStandardItem *item10 = new QStandardItem("张四");
    QStandardItem *item20 = new QStandardItem("张五");

    QStandardItem *item01 = new QStandardItem("男");
    QStandardItem *item11 = new QStandardItem("女");
    QStandardItem *item21 = new QStandardItem("男");

    QStandardItem *item02 = new QStandardItem("15");
    QStandardItem *item12 = new QStandardItem("14");
    QStandardItem *item22 = new QStandardItem("16");

    model->item(0,0)->setChild(0,0,item00);
    model->item(0,0)->setChild(1,0,item10);
    model->item(0,0)->setChild(2,0,item20);

    model->item(0,0)->setChild(0,1,item01);
    model->item(0,0)->setChild(1,1,item11);
    model->item(0,0)->setChild(2,1,item21);

    model->item(0,0)->setChild(0,2,item02);
    model->item(0,0)->setChild(1,2,item12);
    model->item(0,0)->setChild(2,2,item22);


    QStandardItem *item2 = new QStandardItem("五年级");
    model->setItem(1,0,item2);

    QStandardItem *item200 = new QStandardItem("李三");
    QStandardItem *item210 = new QStandardItem("李四");
    QStandardItem *item220 = new QStandardItem("李五");

    QStandardItem *item201 = new QStandardItem("男");
    QStandardItem *item211 = new QStandardItem("女");
    QStandardItem *item221 = new QStandardItem("男");

    QStandardItem *item202 = new QStandardItem("15");
    QStandardItem *item212 = new QStandardItem("14");
    QStandardItem *item222 = new QStandardItem("16");

    model->item(1,0)->setChild(0,0,item200);
    model->item(1,0)->setChild(1,0,item210);
    model->item(1,0)->setChild(2,0,item220);

    model->item(1,0)->setChild(0,1,item201);
    model->item(1,0)->setChild(1,1,item211);
    model->item(1,0)->setChild(2,1,item221);

    model->item(1,0)->setChild(0,2,item202);
    model->item(1,0)->setChild(1,2,item212);
    model->item(1,0)->setChild(2,2,item222);
}

void MainWindow::initModel2(QStandardItemModel *model)
{




    QStandardItem *item1 = new QStandardItem("四年级");


    QStandardItem *item00 = new QStandardItem("张三");
    QStandardItem *item10 = new QStandardItem("张四");
    QStandardItem *item20 = new QStandardItem("张五");

    QStandardItem *item01 = new QStandardItem("男");
    QStandardItem *item11 = new QStandardItem("女");
    QStandardItem *item21 = new QStandardItem("男");

    QStandardItem *item02 = new QStandardItem("15");
    QStandardItem *item12 = new QStandardItem("14");
    QStandardItem *item22 = new QStandardItem("16");

    item1->appendRow(QList<QStandardItem*>() << item00 << item01 << item02);
    item1->appendRow(QList<QStandardItem*>() << item10 << item11 << item12);
    item1->appendRow(QList<QStandardItem*>() << item20 << item21 << item22);

    QStandardItem *item2 = new QStandardItem("五年级");


    QStandardItem *item200 = new QStandardItem("李三");
    QStandardItem *item210 = new QStandardItem("李四");
    QStandardItem *item220 = new QStandardItem("李五");

    QStandardItem *item201 = new QStandardItem("男");
    QStandardItem *item211 = new QStandardItem("女");
    QStandardItem *item221 = new QStandardItem("男");

    QStandardItem *item202 = new QStandardItem("15");
    QStandardItem *item212 = new QStandardItem("14");
    QStandardItem *item222 = new QStandardItem("16");

    item2->appendRow(QList<QStandardItem*>() << item200 << item201 << item202);
    item2->appendRow(QList<QStandardItem*>() << item210 << item211 << item212);
    item2->appendRow(QList<QStandardItem*>() << item220 << item221 << item222);

    model->appendRow(item1);
    model->appendRow(item2);
}

void MainWindow::initModel3(QStandardItemModel *model)
{
    QList<QStandardItem *> parentList = createItemList(ui->lineEdit->text());

    if(parentList.size()>0){

        QList<QStandardItem *>::iterator it = parentList.begin();
        for (;it !=parentList.end();it++) {

            QStandardItem *parent = *it;
            model->appendRow(parent);

            QStandardItem *item = *it;
            QString localText = item->text();

            QList<QStandardItem *> children = createItemList(localText);

            buildTree(parent,children);
        }
    }
}

QList<QStandardItem *> MainWindow::createItemList(QString path)
{
    QList<QStandardItem*> items;
    QDir root(path);
    QFileInfoList localEntryInfoList =
            root.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
    QList<QFileInfo>::const_iterator it = localEntryInfoList.constBegin();
    for(;it != localEntryInfoList.constEnd();it++)
    {
        QFileInfo fileInfo = *it;
        QString path = fileInfo.filePath();
        QStandardItem *item1 = new QStandardItem(path);
        items << item1;
    }
    return items;
}

void MainWindow::buildTree(QStandardItem *parent, QList<QStandardItem *> children)
{
    QList<QStandardItem *>::iterator it = children.begin();
    for (;it !=children.end();it++) {
        QStandardItem *child = *it;
        parent->appendRow(child);

        QString localText = child->text();

        QList<QStandardItem *> children = createItemList(localText);

        buildTree(child,children);
    }
}


//void MainWindow::on_treeView_clicked(const QModelIndex &index)
//{


//}

// 选择文件夹
void MainWindow::on_pushButton_clicked()
{
    // 选择文件夹
    QString imageFolder = QFileDialog::getExistingDirectory(this, tr("选择图片文件路径"), "./", QFileDialog::ShowDirsOnly);


    if(!imageFolder.isEmpty())
    {
        ui->lineEdit->setText(imageFolder);

        QStandardItemModel *model = new QStandardItemModel(this);

        //设置表头隐藏
    //    ui->treeView->setHeaderHidden(true);

        //设置表头
    //    model->setHorizontalHeaderLabels(QStringList()<<"姓名"<<"性别"<<"年龄");

        model->setHorizontalHeaderLabels(QStringList() << "目录");

        //设置model
        ui->treeView->setModel(model);

        //设置展开
        ui->treeView->expandAll();
    //    initModel1(model);
    //    initModel2(model);
        initModel3(model);
    }
}

void MainWindow::on_treeView_clicked(const QModelIndex &index)
{
    int localRow = index.row();
    QVariant localData = index.data();

//    index.column();

    QString string = localData.toString();

    QString msg("%1 %2");
    QString str = msg.arg(localRow).arg(string);
    QMessageBox::information(this,"提示",str);

}
